<?php
$loader->requireOnce('includes/CoreErrorCode.class.php');
$loader->requireOnce('includes/CoreException.class.php');
$loader->requireOnce('includes/LabConnector/LabAssignmentConnector.class.php');
$loader->requireOnce('includes/LabConnector/LabQueueConnector.class.php');
$loader->requireOnce('includes/RadConnector/RadQueueConnector.class.php');
$loader->requireOnce('includes/RadConnector/RadAssignmentConnector.class.php');

/**
 * Controler for processing paraClinicque common task.
 * @package modules.dept_laboratory.controllers
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class ParacliniqueEngine {


	private function __construct() {


	}

	public static function startParaClinicTicket($ticketItemId, $roomId) {
		$logger = LoggerManager::getLogger('local.includes.ParacliniqueEngine');
		$result = array('error'=>array(
            'code'  =>  CoreErrorCode::NORMAL,'mess'=>''),
            'result'=>  ''
            );
            $logger->info('Start actionStartParaClinicTicket ');
            try {

            	$ticketItemObj = Celini::newORDO('TicketItem');
            	$ticketItemObj->setup($ticketItemId);
            	$paraClinicTicketId = $ticketItemObj->get('item_id');
            	$itemType = $ticketItemObj->get('item_type');

            	//Call requested connector
            	switch ($itemType) {
            		case EnumerationConstant::PARACLINIC_LAB:
                        $paraClinicConfigObj = Celini::newOrdo('ParacliniqueConfig');
                        $config = $paraClinicConfigObj->getParaclinicConfig($itemType);
                        if ($roomId != $config['room_id']) {
                             $room = Celini::newORDO('Room', $roomId);
                             $roomName = $room->getLocalizedText('name');
                             $logger->debug('This room [' . $roomName . '] is not [' . $itemType . ']');
                             $logger->info('Finish actionStartParaClinicTicket ');
                             return $result;
                        }
                        //call lab assignment connector to start assignment
                        $labAssignmentConnector = new LabAssignmentConnector();
                        $result = $labAssignmentConnector->startAssignment($paraClinicTicketId);
                        if ($result['error']['code'] == CoreErrorCode::NORMAL) {
                            //call lab queue connector to add patient into lab queue.
                             $labQueueConnector = new LabQueueConnector();
                             $result = $labQueueConnector->addPatient($paraClinicTicketId);
                        }
                        break;
            		case  EnumerationConstant::PARACLINIC_XRAY:
            		case  EnumerationConstant::PARACLINIC_ENDO:
                    case  EnumerationConstant::PARACLINIC_ULTRA:
                        // Add radiology assignment ticket to radiology queue
                        $objConnector   =   new RadQueueConnector();
                        $data['radTicketId']    =   $paraClinicTicketId;
                        $data['roomId']         =   $roomId;
                        $objConnector->addAssignmentTicketToWaitingQueue($data);
            			break;
            		default:
            			;
            			break;
            	}
            } catch (HISFileLoaderException $hisEx) {
            	$result['error']['code'] = CoreErrorCode::LOAD_LAB_MODULE_FAIL;
            	$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::LOAD_LAB_MODULE_FAIL);
            	$logger->error('actionStartParaClinicTicket fail. Error detail: '. $hisEx->getMessage());


            } catch (EnumException $ex) {
            	$result['error']['code'] = $ex->getErrorCode();
            	$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            	$logger->error('actionStartParaClinicTicket fail. Error detail: '. $ex->getMessage());
            } catch (Exception $ex) {
            	$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            	$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            	$logger->error('actionStartParaClinicTicket for ticket fail. Error detail: '. $ex->getMessage());

            }
            $logger->info('Finish actionStartParaClinicTicket ');
            return $result;
	}

    public static function deleteParaClinicTicketItem($ticketItemId) {
    	$logger = LoggerManager::getLogger('local.includes.ParacliniqueEngine');
        $result = array('error'=>array(
            'code'  =>  CoreErrorCode::NORMAL,'mess'=>''),
            'result'=>  ''
            );
            $logger->info('Start deleteParaClinicTicketItem ');
            ///Process for lab
            try {

                $ticketItemObj = Celini::newORDO('TicketItem');
                $ticketItemObj->setup($ticketItemId);
                $paraClinicTicketId = $ticketItemObj->get('item_id');
                $itemType = $ticketItemObj->get('item_type');

                //Call requested connector
                switch ($itemType) {
                    case EnumerationConstant::PARACLINIC_LAB:
                        //call lab assignment connector to start assignment
                        $labAssignmentConnector = new LabAssignmentConnector();
                        $result = $labAssignmentConnector->deleteAssignment($paraClinicTicketId);
                        break;
                    case  EnumerationConstant::PARACLINIC_XRAY:
                    case  EnumerationConstant::PARACLINIC_ENDO:
                    case  EnumerationConstant::PARACLINIC_ULTRA:
                    	$objConnector  =   new RadAssignmentConnector();
                    	$result    =   $objConnector->deleteAssignmentTicket($paraClinicTicketId, $itemType);
                        break;
                    default:
                        ;
                        break;
                }
            } catch (HISFileLoaderException $hisEx) {
                $result['error']['code'] = CoreErrorCode::LOAD_LAB_MODULE_FAIL;
                $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::LOAD_LAB_MODULE_FAIL);
                $logger->error('deleteParaClinicTicketItem fail. Error detail: '. $hisEx->getMessage());


            } catch (EnumException $ex) {
                $result['error']['code'] = $ex->getErrorCode();
                $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
                $logger->error('deleteParaClinicTicketItem fail. Error detail: '. $ex->getMessage());
            } catch (Exception $ex) {
                $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
                $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
                $logger->error('deleteParaClinicTicketItem for ticket fail. Error detail: '. $ex->getMessage());

            }
            $logger->info('Finish deleteParaClinicTicketItem ');
            return $result;

    }

    public static function finishParaClinicTicketItem($ticketItemId) {
    	$logger = LoggerManager::getLogger('local.includes.ParacliniqueEngine');
        $result = array('error'=>array(
            'code'  =>  CoreErrorCode::NORMAL,'mess'=>''),
            'result'=>  ''
        );
        $logger->info('Start finishParaClinicTicketItem ');

        try {
            $ticketItemObj = Celini::newORDO('TicketItem', $ticketItemId);
            $paraClinicTicketId = $ticketItemObj->get('item_id');
            $itemType = $ticketItemObj->get('item_type');

            //Call requested connector
            switch ($itemType) {
                case TicketItem::TEST_TYPE:
                        //call lab assignment connector to start assignment
                    $labAssignmentConnector = new LabQueueConnector();
                    $result = $labAssignmentConnector->finishAssignment($paraClinicTicketId);
                    break;
                case  TicketItem::XRAY_TYPE:
                case  TicketItem::ENDO_TYPE:
                case  TicketItem::ULTRA_TYPE:
                    $objConnector  =   new RadQueueConnector();
                    $result    =   $objConnector->finishAssignment($paraClinicTicketId);
                    break;
                default:
                    break;
            }
        } catch (HISFileLoaderException $hisEx) {
                $result['error']['code'] = CoreErrorCode::LOAD_PARACLINIQUE_MODULE_FAILR;
                $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::LOAD_PARACLINIQUE_MODULE_FAILR);
                $logger->error('finishParaClinicTicketItem fail. Error detail: '. $hisEx->getMessage());


        } catch (EnumException $ex) {
                $result['error']['code'] = $ex->getErrorCode();
                $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
                $logger->error('finishParaClinicTicketItem fail. Error detail: '. $ex->getMessage());
        } catch (Exception $ex) {
                $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
                $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
                $logger->error('finishParaClinicTicketItem for ticket fail. Error detail: '. $ex->getMessage());

        }
        $logger->info('Finish finishParaClinicTicketItem ');
        return $result;
    }
}
?>